package com.thankgod7.jobcenter.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author thankgod7
 */
@Service
@SuppressWarnings("unused")
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 是否存在缓存键值
     *
     * @param key key
     * @return true/false
     */
    public boolean hasKey(String key) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    /**
     * 设置缓存键值
     *
     * @param key   key
     * @param value val
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 获取缓存值
     *
     * @param key key
     * @return val
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置缓存键值
     *
     * @param key      key
     * @param value    val
     * @param timeout  过期时间
     * @param timeUnit 过期时间单位
     */
    public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 删除缓存值
     *
     * @param key key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }
}